package cn.bfay.lion.rest.controller;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import cn.bfay.lion.rest.annotation.Login;
import cn.bfay.lion.rest.param.SetNoticePriceParam;
import cn.bfay.lion.rest.param.WatchOrUnwatchProductParam;
import cn.bfay.lion.rest.service.ProductService;
import cn.bfay.lion.rest.service.UserProductService;
import cn.bfay.lion.rest.vo.CheckWatchVo;
import cn.bfay.lion.rest.vo.ProductDetailInfoPackageVo;
import cn.bfay.lion.rest.vo.ProductInfoVo;
import cn.bfay.lion.rest.vo.UserProductVo;
import cn.bfay.web.result.ListResult;
import cn.bfay.web.result.PageResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品controller.
 *
 * @author wangjiannan
 */
//@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController extends BaseController {
    private final ProductService productService;
    private final UserProductService userProductService;

    /**
     * 获取商品,条件查询+分页.
     *
     * @return {@link UserProductVo}
     */
    @ApiOperation(value = "获取商品", httpMethod = "GET")
    @GetMapping("/get-product-info-by-paging")
    public PageResult<ProductInfoVo> getProductInfoByPaging(@RequestParam(name = "name", defaultValue = "")
                                                            @ApiParam(value = "商品名称") String name,
                                                            @RequestParam(name = "pageNum", defaultValue = "1")
                                                            @ApiParam(value = "页码,默认1") Long pageNum) {
        return productService.getProductInfoByPaging(name, pageNum, 20L);
    }

    /**
     * 获取商品信息.
     *
     * @param skuid 商品id
     * @return {@link ProductDetailInfoPackageVo}
     */
    @ApiOperation(value = "获取商品信息", httpMethod = "GET")
    @GetMapping("/get-product-detail-info-package")
    public ProductDetailInfoPackageVo getProductDetailInfoPackage(
        @RequestParam @ApiParam(value = "商品id") String skuid) {
        return productService.getProductDetailInfoPackage(skuid);
    }

    @Login
    @ApiOperation(value = "关注商品/取消关注商品")
    @PostMapping("/watch-or-unwatch-product")
    public void watchOrUnwatchProduct(@Valid @RequestBody WatchOrUnwatchProductParam watchOrUnwatchProductParam) {
        userProductService.watchOrUnwatchProduct(watchOrUnwatchProductParam);
    }

    /**
     * 获取用户关注商品列表.
     *
     * @return {@link UserProductVo}
     */
    @Login
    @ApiOperation(value = "获取用户关注商品列表")
    @GetMapping("/get-user-products")
    public ListResult<UserProductVo> getUserProducts() {
        return new ListResult<>(userProductService.getUserProducts());
    }

    @Login
    @ApiOperation(value = "设置关注商品的通知价格")
    @PostMapping("/set-notice-price")
    public void setNoticePrice(@Valid @RequestBody SetNoticePriceParam setNoticePriceParam) {
        userProductService.setNoticePrice(setNoticePriceParam);
    }

    @ApiOperation(value = "是否关注当前商品")
    @GetMapping("/check-watch")
    public CheckWatchVo checkWatch(
        @RequestParam @ApiParam(value = "商品id") @NotBlank(message = "商品id不能为空") String skuid) {
        return userProductService.checkWatch(skuid);
    }
}
